<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="iOS," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="Key-value observing(简称KVO)是一种机制，它允许某个对象的属性发生变化时通知其它对象。
Key-value observing主要的好处是，它拥有框架级的支持，使用Key-value observing无需为项目导入其它代码。
不同于NSNotificationCenter的通知，KVO机制中没有负责发送通知的中心对象。当对象的属性发生变化时，该对象直接向所有它的观察者发送通">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS Key-Value Observing(KVO)">
<meta property="og:url" content="http://yoursite.com/2016/10/23/iOS/iOS-Registering-for-Key-Value-Observing-KVO/index.html">
<meta property="og:site_name" content="ZhouJiatao's Blog">
<meta property="og:description" content="Key-value observing(简称KVO)是一种机制，它允许某个对象的属性发生变化时通知其它对象。
Key-value observing主要的好处是，它拥有框架级的支持，使用Key-value observing无需为项目导入其它代码。
不同于NSNotificationCenter的通知，KVO机制中没有负责发送通知的中心对象。当对象的属性发生变化时，该对象直接向所有它的观察者发送通">
<meta property="og:updated_time" content="2017-01-20T03:47:24.493Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS Key-Value Observing(KVO)">
<meta name="twitter:description" content="Key-value observing(简称KVO)是一种机制，它允许某个对象的属性发生变化时通知其它对象。
Key-value observing主要的好处是，它拥有框架级的支持，使用Key-value observing无需为项目导入其它代码。
不同于NSNotificationCenter的通知，KVO机制中没有负责发送通知的中心对象。当对象的属性发生变化时，该对象直接向所有它的观察者发送通">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2016/10/23/iOS/iOS-Registering-for-Key-Value-Observing-KVO/"/>





  <title> iOS Key-Value Observing(KVO) | ZhouJiatao's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  


<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-91431386-1', 'auto');
  ga('send', 'pageview');
</script>









  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">ZhouJiatao's Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle"></p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/10/23/iOS/iOS-Registering-for-Key-Value-Observing-KVO/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="ZhouJiatao">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://7xr5jb.com1.z0.glb.clouddn.com/avatar%23privateinvestocat.jpg">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="ZhouJiatao's Blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="ZhouJiatao's Blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                iOS Key-Value Observing(KVO)
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2016-10-23T12:11:24+00:00">
                2016-10-23
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016/10/23/iOS/iOS-Registering-for-Key-Value-Observing-KVO/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/10/23/iOS/iOS-Registering-for-Key-Value-Observing-KVO/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Key-value observing(简称KVO)是一种机制，它允许某个对象的属性发生变化时通知其它对象。</p>
<p>Key-value observing主要的好处是，它拥有框架级的支持，使用Key-value observing无需为项目导入其它代码。</p>
<p>不同于<code>NSNotificationCenter</code>的通知，KVO机制中没有负责发送通知的中心对象。当对象的属性发生变化时，该对象直接向所有它的观察者发送通知。</p>
<a id="more"></a>
<!-- 相关知识：key-value coding（链接到另一篇博客）//TODO 
－－－
-->
<h1 id="注册Key-Value-Observing"><a href="#注册Key-Value-Observing" class="headerlink" title="注册Key-Value Observing"></a>注册Key-Value Observing</h1><p>为了正常接收到Key-Value Observing通知，必须遵循3个步骤:  </p>
<ol>
<li>被观察对象调用<code>addObserver:forKeyPath:options:context:</code>方法添加观察者.  </li>
<li>观察对象(接收通知的一方)内部实现 <code>observeValueForKeyPath:ofObject:change:context:</code>方法用于接收通知.  </li>
<li>当观察者不想(或不能)接收通知时，使用<code>removeObserver:forKeyPath:</code>方法注销。至少需要在观察者从内存释放之前调用该方法。  </li>
</ol>
<h2 id="step1-注册为观察者"><a href="#step1-注册为观察者" class="headerlink" title="step1:注册为观察者"></a>step1:注册为观察者</h2><p>被观察对象调用<code>addObserver:forKeyPath:options:context:</code>方法添加观察者.<br>eg: 将<code>self</code>注册为观察者，监听<code>account</code>对象的<code>balance</code>和<code>interestRate</code>属性。  </p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//Creating context pointers</span></div><div class="line"><span class="keyword">static</span> <span class="keyword">void</span> *PersonAccountBalanceContext = &amp;PersonAccountBalanceContext;</div><div class="line"><span class="keyword">static</span> <span class="keyword">void</span> *PersonAccountInterestRateContext = &amp;PersonAccountInterestRateContext;</div><div class="line"></div><div class="line"><span class="comment">//Registering sef as an Observer</span></div><div class="line">- (<span class="keyword">void</span>)registerAsObserverForAccount:(Account*)account &#123;</div><div class="line">    [account addObserver:<span class="keyword">self</span></div><div class="line">              forKeyPath:<span class="string">@"balance"</span></div><div class="line">                 options:(<span class="built_in">NSKeyValueObservingOptionNew</span> |</div><div class="line">                          <span class="built_in">NSKeyValueObservingOptionOld</span>)</div><div class="line">                 context:PersonAccountBalanceContext];</div><div class="line"> </div><div class="line">    [account addObserver:<span class="keyword">self</span></div><div class="line">              forKeyPath:<span class="string">@"interestRate"</span></div><div class="line">                 options:(<span class="built_in">NSKeyValueObservingOptionNew</span> |</div><div class="line">                          <span class="built_in">NSKeyValueObservingOptionOld</span>)</div><div class="line">                  context:PersonAccountInterestRateContext];</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="Options"><a href="#Options" class="headerlink" title="Options"></a>Options</h3><p>Options参数用于指定接收什么类型的通知、通知需要携带什么内容。  </p>
<h4 id="NSKeyValueObservingOptions"><a href="#NSKeyValueObservingOptions" class="headerlink" title="NSKeyValueObservingOptions:"></a>NSKeyValueObservingOptions:</h4><ul>
<li>NSKeyValueObservingOptionNew: 指明通知中应该包含 属性的新值。  </li>
<li>NSKeyValueObservingOptionOld: 指明通知中应该包含 属性的旧值。  </li>
<li>NSKeyValueObservingOptionInitial: 一旦指明此option,被观测对象会立刻发送通知(在<code>addObserver:forKeyPath:options:context:</code>return之前)。通常用于获取属性的初始值。  </li>
<li>NSKeyValueObservingOptionPrior: 指明在属性每次变化前和变化后都发送通知。并且通知中的change dictionary 总是包含<code>NSKeyValueChangeNotificationIsPriorKey</code>条目，值为<code>NSNumber</code>封装的<code>bool</code>类型,等于<code>@YES</code>表明接收到的是变化前的通知，等于<code>@NO</code>表明接收到的是变化后的通知。  </li>
</ul>
<h3 id="Context"><a href="#Context" class="headerlink" title="Context"></a>Context</h3><p>Context参数是一个指针，会传回给观察者的回调方法，作为唯一标识使用。大多数情况下,在回调方法内使用key path字符串就可以区分不同的通知，所以Context参数通常会设置为<code>nil</code>或者<code>NULL</code>。  </p>
<h2 id="step2-接收通知"><a href="#step2-接收通知" class="headerlink" title="step2:接收通知"></a>step2:接收通知</h2><p>观察对象必须实现<code>observeValueForKeyPath:ofObject:change:context:</code>方法。<br>当被观察属性的值发生变化时，观察对象的<code>observeValueForKeyPath:ofObject:change:context:</code>方法会被回调。  </p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// Implementation of observeValueForKeyPath:ofObject:change:context:</span></div><div class="line">- (<span class="keyword">void</span>)observeValueForKeyPath:(<span class="built_in">NSString</span> *)keyPath</div><div class="line">                      ofObject:(<span class="keyword">id</span>)object</div><div class="line">                        change:(<span class="built_in">NSDictionary</span> *)change</div><div class="line">                       context:(<span class="keyword">void</span> *)context &#123;</div><div class="line"> </div><div class="line">    <span class="keyword">if</span> (context == PersonAccountBalanceContext) &#123;</div><div class="line">        <span class="comment">// Do something with the balance…</span></div><div class="line"> </div><div class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (context == PersonAccountInterestRateContext) &#123;</div><div class="line">        <span class="comment">// Do something with the interest rate…</span></div><div class="line"> </div><div class="line">    &#125; <span class="keyword">else</span> &#123;</div><div class="line">        <span class="comment">// Any unrecognized context must belong to super</span></div><div class="line">        [<span class="keyword">super</span> observeValueForKeyPath:keyPath</div><div class="line">                             ofObject:object</div><div class="line">                               change:change</div><div class="line">                               context:context];</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在本示例中，通过key path足以判断是哪个对象的属性发生了变化,所以可以这样:<br><figure class="highlight objectivec"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="comment">// Implementation of observeValueForKeyPath:ofObject:change:context:</span></div><div class="line">- (<span class="keyword">void</span>)observeValueForKeyPath:(<span class="built_in">NSString</span> *)keyPath</div><div class="line">                      ofObject:(<span class="keyword">id</span>)object</div><div class="line">                        change:(<span class="built_in">NSDictionary</span> *)change</div><div class="line">                       context:(<span class="keyword">void</span> *)context &#123;</div><div class="line"> </div><div class="line">    <span class="keyword">if</span> ([keyPath isEqualToString:<span class="string">@"balance"</span>]) &#123;</div><div class="line">        <span class="comment">// Do something with the balance…</span></div><div class="line"> </div><div class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> ([keyPath isEqualToString:<span class="string">@"interestRate"</span>]) &#123;</div><div class="line">        <span class="comment">// Do something with the interest rate…</span></div><div class="line"> </div><div class="line">    &#125; <span class="keyword">else</span> &#123;</div><div class="line">        <span class="comment">// Any unrecognized context must belong to super</span></div><div class="line">        [<span class="keyword">super</span> observeValueForKeyPath:keyPath</div><div class="line">                             ofObject:object</div><div class="line">                               change:change</div><div class="line">                               context:context];</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h1 id="step3-移除观察者"><a href="#step3-移除观察者" class="headerlink" title="step3:移除观察者"></a>step3:移除观察者</h1><p>被观察对象调用<code>removeObserver:forKeyPath:context:</code>方法移除观察对象。  </p>
<figure class="highlight objectivec"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">- (<span class="keyword">void</span>)unregisterAsObserverForAccount:(Account*)account &#123;</div><div class="line">    [account removeObserver:<span class="keyword">self</span></div><div class="line">                 forKeyPath:<span class="string">@"balance"</span></div><div class="line">                    context:PersonAccountBalanceContext];</div><div class="line"> </div><div class="line">    [account removeObserver:<span class="keyword">self</span></div><div class="line">                 forKeyPath:<span class="string">@"interestRate"</span></div><div class="line">                    context:PersonAccountInterestRateContext];</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="常见问题"><a href="#常见问题" class="headerlink" title="常见问题"></a>常见问题</h1><h2 id="报错-libc-abi-dylib-terminate-handler-unexpectedly-threw-an-exception"><a href="#报错-libc-abi-dylib-terminate-handler-unexpectedly-threw-an-exception" class="headerlink" title="报错:libc++abi.dylib: terminate_handler unexpectedly threw an exception"></a>报错:libc++abi.dylib: terminate_handler unexpectedly threw an exception</h2><p>发生于<em>被观察者</em>向一个不存在的对象发送通知，所以，一定不要忘记在合适的时候进行 removeObserver 操作，至少在观察者内存被释放之前。<br>此错误最常发生于，ViewController pop 后或 dismiss 后。</p>
<h2 id="KVO在swift中不管用"><a href="#KVO在swift中不管用" class="headerlink" title="KVO在swift中不管用?"></a>KVO在swift中不管用?</h2><p>对于自定义的 class，首先需要确保该 class 是 <code>NSObject</code> 的子类；<br>其次，需要使用dynamy关键字修饰被察者属性；<br>比如，我们自定义Person类，希望它的birthday属性可以被观察:  </p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span>: <span class="title">NSObject</span> </span>&#123;</div><div class="line">    <span class="keyword">dynamic</span> <span class="keyword">var</span> birthday = <span class="type">NSDate</span>()</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="适合在ViewController-addObserver／removeObserver-的时机？"><a href="#适合在ViewController-addObserver／removeObserver-的时机？" class="headerlink" title="适合在ViewController addObserver／removeObserver 的时机？"></a>适合在ViewController addObserver／removeObserver 的时机？</h2><p>个人推荐通常情况下在 viewDidAppear 中 addObserver: 此时视图都加载完毕，并显示出来。<br>在 viewDidDisappear 中 removeObserver：此时视图已经消失，并且视图对象可能已经销毁。<br>这只是通常情况，你需要根据自己的需求选择最合适的时机。  </p>
<p>另一个建议是，不要在 viewWillAppear、viewWillAppear 中 addObserver、removeObserver:<br>因为一个 ViewController 作为 subcontroller 时，其 viewWillAppear、viewWillAppear并不会在每次视图显示、消失时被调用。  </p>
<p>参考链接:<br><a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html#//apple_ref/doc/uid/10000177-BCICJDHA" target="_blank" rel="external">Introduction to Key-Value Observing Programming Guide</a><br><a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueObserving/Articles/KVOBasics.html#//apple_ref/doc/uid/20002252-BAJEAIEE" target="_blank" rel="external">Registering for Key-Value Observing</a><br><a href="http://www.appcoda.com/understanding-key-value-observing-coding/" target="_blank" rel="external">Understanding Key-Value Observing and Coding</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/iOS/" rel="tag"># iOS</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/09/21/Other/搭建Git服务器/" rel="next" title="搭建Git服务器">
                <i class="fa fa-chevron-left"></i> 搭建Git服务器
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/01/20/iOS/UIView如何移除所有subview/" rel="prev" title="UIView如何移除所有subview">
                UIView如何移除所有subview <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/10/23/iOS/iOS-Registering-for-Key-Value-Observing-KVO/"
           data-title="iOS Key-Value Observing(KVO)" data-url="http://yoursite.com/2016/10/23/iOS/iOS-Registering-for-Key-Value-Observing-KVO/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://7xr5jb.com1.z0.glb.clouddn.com/avatar%23privateinvestocat.jpg"
               alt="ZhouJiatao" />
          <p class="site-author-name" itemprop="name">ZhouJiatao</p>
          <p class="site-description motion-element" itemprop="description">Write simple code</p>
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">29</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">18</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/RealMeZJT" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#注册Key-Value-Observing"><span class="nav-number">1.</span> <span class="nav-text">注册Key-Value Observing</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#step1-注册为观察者"><span class="nav-number">1.1.</span> <span class="nav-text">step1:注册为观察者</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Options"><span class="nav-number">1.1.1.</span> <span class="nav-text">Options</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#NSKeyValueObservingOptions"><span class="nav-number">1.1.1.1.</span> <span class="nav-text">NSKeyValueObservingOptions:</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Context"><span class="nav-number">1.1.2.</span> <span class="nav-text">Context</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#step2-接收通知"><span class="nav-number">1.2.</span> <span class="nav-text">step2:接收通知</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#step3-移除观察者"><span class="nav-number">2.</span> <span class="nav-text">step3:移除观察者</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#常见问题"><span class="nav-number">3.</span> <span class="nav-text">常见问题</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#报错-libc-abi-dylib-terminate-handler-unexpectedly-threw-an-exception"><span class="nav-number">3.1.</span> <span class="nav-text">报错:libc++abi.dylib: terminate_handler unexpectedly threw an exception</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#KVO在swift中不管用"><span class="nav-number">3.2.</span> <span class="nav-text">KVO在swift中不管用?</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#适合在ViewController-addObserver／removeObserver-的时机？"><span class="nav-number">3.3.</span> <span class="nav-text">适合在ViewController addObserver／removeObserver 的时机？</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ZhouJiatao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"realmezjt"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  












  
  

  

  

  

  


</body>
</html>
